package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"strings"
)

func main() {
	var site = flag.String("site", "", "site name in web.toml")
	var sql = flag.String("sql", "", "sql file pathname")
	flag.Parse()
	fp, err := os.Open(*sql)
	if err != nil {
		panic(err)
	}
	defer fp.Close()
	buf, _ := ioutil.ReadAll(fp)
	vs := strings.Split(strings.TrimSpace(string(buf)), ";")
	var sqls []string
	for i, s := range vs[0 : len(vs)-1] {
		fmt.Println(i, strings.TrimSpace(s))
		sqls = append(sqls, strings.TrimSpace(s))
	}

	conf1 := readToml("web.toml")
	cfg, ok := conf1.Sites[*site]
	if !ok {
		panic("must run with '-site sitename' flag")
	}
	db1 := NewMyDB(cfg["Addr"], cfg["User"], cfg["Password"], cfg["Database"])
	db1.ExecSqls(vs[0 : len(vs)-1])
	db1.Close()
}
